package com.xiecong.controller;

import com.xiecong.aspect.annotation.LogOperation;
import com.xiecong.pojo.Emp;
import com.xiecong.pojo.EmpQueryParam;
import com.xiecong.pojo.PageResult;
import com.xiecong.pojo.Result;
import com.xiecong.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;


    /**
     * 分页查询
     */
    /*@GetMapping
    public Result findByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "1") Integer pageSize) {
        log.info("分页查询:page = {},pageSize = {}", page, pageSize);
        PageResult<Emp> pageResult = empService.findByPage(page, pageSize);
        return Result.success(pageResult);
    }*/


    /**
     * 条件分页查询
     */
    @GetMapping
    // 用查找型封装查询条件  /emps?name=张&gender=1&begin=2007-09-01&end=2022-09-01&page=1&pageSize=10
    public Result pageQuery(EmpQueryParam empQueryParam){
        log.info("条件分页查询:{}",empQueryParam);
        PageResult<Emp> empPageResult = empService.pageQuery(empQueryParam);
        return Result.success(empPageResult);
    }


    /**
     * 添加员工
     */
    @PostMapping
    @LogOperation
    public Result add(@RequestBody Emp emp){
        log.info("添加员工:{}",emp);
        empService.add(emp);
        return Result.success();
    }


    /**
     * 删除员工
     */
    @DeleteMapping
    @LogOperation
    public Result delete(@RequestParam List<Integer> ids){
        log.info("删除员工:{}",ids);
        empService.deleteByIds(ids);
        return Result.success();
    }


    /**
     * 修改员工-数据回显-根据id查询
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        Emp emp = empService.findById(id);
        return Result.success(emp);
    }

    /**
     * 修改员工-修改员工信息
     */
    @PutMapping
    @LogOperation
    public Result update(@RequestBody Emp emp){
        log.info("修改员工信息:{}",emp);
        empService.updateById(emp);
        return Result.success();
    }


    /**
     * 查询所有员工信息-添加班级中班主任显示
     */
    @GetMapping("/list")
    public Result findAll(){
        log.info("查询所有员工信息");
        List<Emp> empList = empService.findAll();
        return Result.success(empList);
    }
}
